#!/bin/bash
#
# rm-remote-tags -- remove tags from remote docker registry
#
# Copyright (C) 2016-2021 Wu Zhangjin <falcon@ruma.tech>
#
# ref: https://devopsheaven.com/docker/dockerhub/2018/04/09/delete-docker-image-tag-dockerhub.html
#

echo
echo "LOG: Remove tags from remote hub.docker.com, use the official docker service."
echo

echo "LOG: Please input necessary information:"
echo

read -p "Docker Username: " DOCKER_USERNAME
read -p "Docker password: " DOCKER_PASSWORD
read -p "Docker Organization: " DOCKER_ORGANIZATION
read -p "Docker Image: " DOCKER_IMAGE

# docker run --rm -it lumir/remove-dockerhub-tag --user "$DOCKER_USERNAME" --password "$DOCKER_PASSWORD" $DOCKER_TAGS

login_data() {
cat <<EOF
{
    "username": "$DOCKER_USERNAME",
    "password": "$DOCKER_PASSWORD"
}
EOF
}

# Get a token

TOKEN=`curl -s -H "Content-Type: application/json" -X POST -d "$(login_data)" "https://hub.docker.com/v2/users/login/" | jq -r .token`

# Allow remove many tags
while :; do
  echo
  echo "INFO: Current Tags"
  curl -s "https://registry.hub.docker.com/v2/repositories/${DOCKER_ORGANIZATION}/${DOCKER_IMAGE}/tags/" | jq '."results"[]["name"]'
  echo

  while :; do
    echo
    echo "INFO: Please input the tag to remove, if no one required to remove, just press Enter key:"
    echo
    read -p "Docker Tag: " DOCKER_TAG
    echo

    if [ -z "$DOCKER_TAG" ]; then
      read -p "LOG: No tag specified, do you want to exit? (Y/N) " sure
      [ "$sure" = "Y" -o "$sure" = "y" ] && exit 0
    else
      break
    fi
  done

  # Removing tags
  echo
  echo "LOG: Remove ${DOCKER_ORGANIZATION}/${DOCKER_IMAGE}:${DOCKER_TAG}"
  echo

  curl "https://hub.docker.com/v2/repositories/${DOCKER_ORGANIZATION}/${DOCKER_IMAGE}/tags/${DOCKER_TAG}/" \
  -X DELETE \
  -H "Authorization: JWT ${TOKEN}"
done
